import StringIO

def path(filename):
    return 'data/' + filename

class MockWriter:
    def __init__(self, writer):
        self.writer = writer
        self.writer._open = self.mock_open
        self.writer._close = self.mock_close
        self._filenames = {}
        self._contents = {}

    def mock_open(self, filename, _):
        buffer = StringIO.StringIO()
        self._filenames[buffer] = filename
        return buffer

    def mock_close(self, file):
        assert self._filenames.has_key(file), 'multiple close()? %s' % file
        filename = self._filenames[file]
        self._contents[filename] = file.getvalue()
        file.close()
        del self._filenames[file]

    def write(self, filename, streams, log):
        self.writer.write(filename, streams, log)

    def flush(self):
        self.writer.flush()

    def get_contents(self, filename):
        if self._contents.has_key(filename):
            return self._contents[filename]
        return None

    def dump_contents(self):
        dump = ''
        for filename, contents in self._contents.iteritems():
            dump += '"%s" contains "%s"\n\n' % (filename, contents)

class MockMkdirP:
    def __init__(self, writer):
        self._last_dir = None
        writer._mkdir_p = self.mock_mkdir_p

    def mock_mkdir_p(self, dir):
        self._last_dir = dir

    def get_last_dir(self):
        return self._last_dir
